#include "usermanagerhidecolumndlg.h"
#include "ui_usermanagerhidecolumndlg.h"

UserManagerHideColumnDlg::UserManagerHideColumnDlg(QStringList *list,QTableWidget *t,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UserManagerHideColumnDlg)
{
    ui->setupUi(this);
    tableWidget = t;
    ui->tableWidget_right->setColumnCount(1);

    ui->label__leftCount->setText("0/" + QString::number(list->size()));
    ui->tableWidget_left->setColumnCount(1);
    for(int i = 0;i< list->size();i++){
        ui->tableWidget_left->insertRow(ui->tableWidget_left->rowCount());
        ui->tableWidget_left->setItem(ui->tableWidget_left->rowCount() - 1,0,createWidgetItem(list->at(i)));
    }
}

UserManagerHideColumnDlg::~UserManagerHideColumnDlg()
{
    delete ui;
}

QTableWidgetItem *UserManagerHideColumnDlg::createWidgetItem(const QString &labelText){
    QTableWidgetItem * w = new QTableWidgetItem;
    w->setText(labelText);
    return w;
}

void UserManagerHideColumnDlg::on_tableWidget_left_itemSelectionChanged()
{
    leftItems = ui->tableWidget_left->selectedItems();
    if(leftItems.size() > 0){
        ui->label__leftCount->setText(QString::number(leftItems.size()) + "/" + QString::number(ui->tableWidget_left->rowCount()));
        ui->btn_right->setEnabled(1);
    }
}

void UserManagerHideColumnDlg::on_btn_right_clicked()
{
    const QList<QTableWidgetItem*> l = leftItems;
    for(auto w : l){
        for (int col = 0; col < tableWidget->columnCount(); col++) {
            QTableWidgetItem* headerItem = tableWidget->horizontalHeaderItem(col);
            if (headerItem && headerItem->text() == w->text()) {
                tableWidget->hideColumn(col);
            }
        }
        ui->tableWidget_right->insertRow(ui->tableWidget_right->rowCount());
        ui->tableWidget_right->setItem(ui->tableWidget_right->rowCount() - 1,0,createWidgetItem(w->text()));
        ui->tableWidget_left->removeRow(w->row());
    }
    leftItems.clear();
    ui->label__leftCount->setText(QString::number(leftItems.size())+"/"+QString::number(ui->tableWidget_left->rowCount()));
    ui->label_rightCount->setText(QString::number(rightItems.size())+"/"+QString::number(ui->tableWidget_right->rowCount()));
    ui->btn_right->setEnabled(0);
    ui->checkBox_left->setChecked(0);
}

void UserManagerHideColumnDlg::on_tableWidget_right_itemSelectionChanged()
{
    rightItems = ui->tableWidget_right->selectedItems();
    if(rightItems.size() > 0){
        ui->label_rightCount->setText(QString::number(rightItems.size()) + "/" + QString::number(ui->tableWidget_right->rowCount()));
        ui->btn_left->setEnabled(1);
    }
}

void UserManagerHideColumnDlg::on_btn_left_clicked()
{
    const QList<QTableWidgetItem*> l = rightItems;
    for(auto w : l){
        for (int col = 0; col < tableWidget->columnCount(); col++) {
            QTableWidgetItem* headerItem = tableWidget->horizontalHeaderItem(col);
            if (headerItem && headerItem->text() == w->text()) {
                tableWidget->showColumn(col);
            }
        }
        ui->tableWidget_left->insertRow(ui->tableWidget_left->rowCount());
        ui->tableWidget_left->setItem(ui->tableWidget_left->rowCount() - 1,0,createWidgetItem(w->text()));
        ui->tableWidget_right->removeRow(w->row());
    }
    rightItems.clear();
    ui->label__leftCount->setText(QString::number(leftItems.size())+"/"+QString::number(ui->tableWidget_left->rowCount()));
    ui->label_rightCount->setText(QString::number(rightItems.size())+"/"+QString::number(ui->tableWidget_right->rowCount()));
    ui->btn_left->setEnabled(0);
    ui->checkBox_right->setChecked(0);
}



void UserManagerHideColumnDlg::on_checkBox_left_stateChanged(int arg1)
{
    if(arg1 == 2){
        ui->tableWidget_left->selectAll();
    }else{
        ui->tableWidget_left->clearSelection();
    }
}


void UserManagerHideColumnDlg::on_checkBox_right_stateChanged(int arg1)
{
    if(arg1 == 2){
        ui->tableWidget_right->selectAll();
    }else{
        ui->tableWidget_right->clearSelection();
    }
}

